//
//  UIView+Loading.m
//  Sekiss
//
//  Created by Alex on 15-3-3.
//  Copyright (c) 2015年 Alex. All rights reserved.
//

#import "UIView+Loading.h"
#import "DKButton.h"

@interface UIView()

@property UIActivityIndicatorView* loadingView;//加载动画
@property UILabel* contentLabel;//文字说明
@property DKButton* retryButton;//重按的按钮
@property UIView* backgroundView;//背景遮罩

@end

@implementation UIView (Loading)



#pragma mark- 控件的动态绑定
static char DKLoadingViewKey;
static char DKLoadingLabelKey;
static char DKLoadingButtonKey;
static char DKLoadingBackgroundKey;
-(UIActivityIndicatorView*)loadingView
{
    return objc_getAssociatedObject(self, &DKLoadingViewKey);
}
-(void)setLoadingView:(UIActivityIndicatorView *)loadingView
{
    [self willChangeValueForKey:@"DKLoadingViewKey"];
    objc_setAssociatedObject(self, &DKLoadingViewKey,
                             loadingView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"DKLoadingViewKey"];
}
//------------------------------
-(UILabel*)contentLabel
{
    return objc_getAssociatedObject(self, &DKLoadingLabelKey);
}
-(void)setContentLabel:(UILabel *)contentLabel
{
    [self willChangeValueForKey:@"DKLoadingLabelKey"];
    objc_setAssociatedObject(self, &DKLoadingLabelKey,
                             contentLabel,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"DKLoadingLabelKey"];
}
//------------------------------
-(DKButton*)retryButton
{
    return objc_getAssociatedObject(self, &DKLoadingButtonKey);
}
-(void)setRetryButton:(DKButton *)retryButton
{
    [self willChangeValueForKey:@"DKLoadingButtonKey"];
    objc_setAssociatedObject(self, &DKLoadingButtonKey,
                             retryButton,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"DKLoadingButtonKey"];
}
//------------------------------
-(UIView*)backgroundView
{
    return objc_getAssociatedObject(self, &DKLoadingBackgroundKey);
}
-(void)setBackgroundView:(UIView *)backgroundView
{
    [self willChangeValueForKey:@"DKLoadingBackgroundKey"];
    objc_setAssociatedObject(self, &DKLoadingBackgroundKey,
                             backgroundView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"DKLoadingBackgroundKey"];
}
//------------------------------
-(void)refreshUI
{
    //loading图标
    if(self.loadingView==nil)
    {
        self.loadingView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.loadingView startAnimating];
    }
    else
    {
        [self.loadingView removeFromSuperview];
    }
    //提示文字
    if(self.contentLabel==nil)
    {
        self.contentLabel=[[UILabel alloc] init];
        [self.contentLabel setTextAlignment:NSTextAlignmentCenter];
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.contentLabel setFont:[UIFont systemFontOfSize:18.0f]];
        [self.contentLabel setNumberOfLines:0];
    }
    {
        [self.contentLabel removeFromSuperview];
    }
    
    //重试按钮
    if(self.retryButton==nil)
    {
        self.retryButton=[[DKButton alloc] init];
        [self.retryButton setText:@"重新加载"];
        [self.retryButton setBackgroundColor:[UIColor colorWithRed:209/255.0 green:209/255.0 blue:209/255.0 alpha:1.0]];
        [self.retryButton setTextColor:[UIColor whiteColor]];
        [self.retryButton setFont:[UIFont boldSystemFontOfSize:18.0f]];
    }
    else
    {
        [self.retryButton endLoading];
        [self.retryButton removeFromSuperview];
    }
    
    //背景遮罩
    if(self.backgroundView==nil)
    {
        self.backgroundView=[[UIView alloc] init];
    }
    else
    {
        [self.backgroundView removeFromSuperview];
    }
}



#pragma mark- 无文字显示
-(void)startLoading
{
    [self startLoadingWithMask:UIViewLoadingTypeDefault];
}
-(void)startLoadingWithMask:(UIViewLoadingType)type
{
    [self refreshUI];
    
    
    //背景遮罩
    self.backgroundView.frame=CGRectMake(0,
                                         0,
                                         self.frame.size.width,
                                         self.frame.size.height);
    [self addSubview:self.backgroundView];
   // [self.layer addSublayer:self.backgroundView.layer];
    //加载条
    self.loadingView.frame=CGRectMake(0,
                                      0,
                                      self.frame.size.width,
                                      self.frame.size.height);
    [self.backgroundView addSubview:self.loadingView];
    
    
    if(type==UIViewLoadingTypeDefault)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [self.backgroundView setUserInteractionEnabled:YES];
        [self.backgroundView setBackgroundColor:[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0]];
    }
    else if(type==UIViewLoadingTypeBlack)
    {
        [self.contentLabel setTextColor:[UIColor whiteColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
        [self.backgroundView setUserInteractionEnabled:NO];
        [self.backgroundView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
    }
    else if(type==UIViewLoadingTypeClear)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [self.backgroundView setUserInteractionEnabled:NO];
        [self.backgroundView setBackgroundColor:[UIColor clearColor]];
    }
}

#pragma mark- 有文字显示
-(void)startLoadingWithText:(NSString *)text
{
    [self startLoadingWithText:text Mask:UIViewLoadingTypeDefault];
}
-(void)startLoadingWithText:(NSString *)text Mask:(UIViewLoadingType)type
{
    [self refreshUI];
    
    
    //背景遮罩
    self.backgroundView.frame=CGRectMake(0,
                                         0,
                                         self.frame.size.width,
                                         self.frame.size.height);
    [self addSubview:self.backgroundView];
    
    
    CGFloat loadingSize=50;//此处加载图标的size默认为50*50
    CGFloat gap=10;//此处文字和加载图标的间隔
    //计算文字所占大小
    NSDictionary *attributes = @{NSFontAttributeName:self.contentLabel.font};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.frame.size.width-80, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    //设置文字的位置
    self.contentLabel.frame=CGRectMake(40,
                                       (self.frame.size.height-rect.size.height-loadingSize-gap)/2,
                                       self.frame.size.width-80,
                                       rect.size.height);
    self.contentLabel.text=text;
    [self.backgroundView addSubview:self.contentLabel];
    
    //加载条
    self.loadingView.frame=CGRectMake((self.frame.size.width-loadingSize)/2,
                                      self.contentLabel.frame.size.height+self.contentLabel.frame.origin.y+gap,
                                      loadingSize,
                                      loadingSize);
    [self.backgroundView addSubview:self.loadingView];
    

    if(type==UIViewLoadingTypeDefault)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [self.backgroundView setUserInteractionEnabled:YES];
        [self.backgroundView setBackgroundColor:[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0]];
    }
    else if(type==UIViewLoadingTypeBlack)
    {
        [self.contentLabel setTextColor:[UIColor whiteColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
        [self.backgroundView setUserInteractionEnabled:NO];
        [self.backgroundView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
    }
    else if(type==UIViewLoadingTypeClear)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [self.backgroundView setUserInteractionEnabled:NO];
        [self.backgroundView setBackgroundColor:[UIColor clearColor]];
    }
}
//解除显示
-(void)stopLoading
{
    [self refreshUI];
}


#pragma mark- 显示重试按钮
-(void)showRetryWithText:(NSString*)text OnClick:(void (^)())click
{
    [self showRetryWithText:text Mask:UIViewRetryTypeDefault OnClick:click];
}
-(void)showRetryWithText:(NSString*)text Mask:(UIViewRetryType)type OnClick:(void (^)())click
{
    [self refreshUI];
    
    
    //背景遮罩
    self.backgroundView.frame=CGRectMake(0,
                                         0,
                                         self.frame.size.width,
                                         self.frame.size.height);
    [self addSubview:self.backgroundView];
    
    
    CGFloat loadingSize=40;//按钮高度
    CGFloat gap=10;//此处文字和加载图标的间隔
    //计算文字所占大小
    NSDictionary *attributes = @{NSFontAttributeName:self.contentLabel.font};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.frame.size.width-80, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    //设置文字的位置
    self.contentLabel.frame=CGRectMake(40,
                                       (self.frame.size.height-rect.size.height-loadingSize-gap)/2,
                                       self.frame.size.width-80,
                                       rect.size.height);
    self.contentLabel.text=text;
    [self.backgroundView addSubview:self.contentLabel];
    
    //重新加载按钮
    self.retryButton.frame=CGRectMake(40,
                                      self.contentLabel.frame.size.height+self.contentLabel.frame.origin.y+gap,
                                      self.frame.size.width-80,
                                      loadingSize);
    [self.retryButton addBlock:^(DKButton* sender)
     {
         [sender startLoading];
         click();
     }
                      ForEvent:DKButtonEventPressedEnd];
    [self.backgroundView addSubview:self.retryButton];
    
    
    [self.backgroundView setUserInteractionEnabled:YES];
    if(type==UIViewRetryTypeDefault)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.backgroundView setBackgroundColor:[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0]];
    }
    else if(type==UIViewRetryTypeBlack)
    {
        [self.contentLabel setTextColor:[UIColor whiteColor]];
        [self.backgroundView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f]];
    }
    else if(type==UIViewRetryTypeClear)
    {
        [self.contentLabel setTextColor:[UIColor grayColor]];
        [self.backgroundView setBackgroundColor:[UIColor clearColor]];
    }
}

@end
